package com.example.entry;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import java.io.File;

public class BodyEntry {


    private  StringProperty name = new SimpleStringProperty("");

    private  StringProperty type = new SimpleStringProperty("字符串");

    private  ObjectProperty<Object> value = new SimpleObjectProperty<>("");

    private String fullPath;  // 用于存储文件的完整路径

    public String getFullPath() {
        return fullPath;
    }

    public void setFullPath(String fullPath) {
        this.fullPath = fullPath;
    }

    public BodyEntry() {
    }

    public BodyEntry(String name, String type, Object value) {
        setName(name);
        setType(type);
        setValue(value);
    }

    // JavaFX Properties

    public StringProperty nameProperty() { return name; }

    public StringProperty typeProperty() { return type; }

    public ObjectProperty<Object> valueProperty() { return value; }

    // Getters and Setters for JSON
    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        if (this.name != null) {
            this.name.set(name);
        }
    }

    public String getType() {
        return type.get();
    }

    public void setType(String type) {
        if (this.type != null) {
            this.type.set(type);
        }
    }

    public Object getValue() {
        return value.get();
    }

    public void setValue(Object value) {
        if (this.value != null) {
            if ("文件".equals(getType()) && value instanceof String) {
                File file = new File(value.toString());
                if (file.exists()) {
                    this.value.set(file);
                    this.fullPath = file.getAbsolutePath();
                } else {
                    this.value.set(value);
                }
            } else {
                this.value.set(value);
            }
        }
    }

    // 辅助方法
    public String getDisplayValue() {
        Object val = getValue();
        if (val == null) return "";

        if ("文件".equals(getType())) {
            if (val instanceof File) {
                return ((File) val).getName();
            } else if (val instanceof String) {
                File file = new File(val.toString());
                return file.getName();
            }
        }
        return val.toString();
    }

    public File getFile() {
        Object val = getValue();
        if (val instanceof File) {
            return (File) val;
        }
        return null;
    }

    // 添加一个方法来获取实际的值（用于发送请求时）
    public Object getActualValue() {
        Object val = getValue();
        if (val == null) return "";

        if ("文件".equals(getType())) {
            if (val instanceof File) {
                return val;  // 返回File对象
            } else if (val instanceof String && !((String) val).isEmpty()) {
                return new File(val.toString());  // 尝试转换为File对象
            }
        }
        return val;  // 返回原始值
    }

    @Override
    public String toString() {
        return String.format("BodyEntry{name=%s, type=%s, value=%s}",
            getName(), getType(), getDisplayValue());
    }
}
